package com.mvc.Impl;

import java.io.Serializable;

import java.util.Date;
import java.util.List;

import org.hibernate.*;
import org.hibernate.type.StandardBasicTypes;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.mvc.dao.EntityDao;

import javax.annotation.Resource;

public class EntityDaoImpl extends HibernateDaoSupport implements EntityDao {
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	public int countAll(String clazz) {
		final String hql = "select count(*) from " + clazz + " as a";
		Session session = sessionFactory.openSession();

		Query query = session.createSQLQuery(hql);
		query.setMaxResults(1);
		Long count = (Long) query.uniqueResult();
		if (!Hibernate.isInitialized(count))
			Hibernate.initialize(count);
		session.close();
		if (count != null) {
			return count.intValue();
		} else {
			return 0;
		}
	}

	public int countQuery(String hql) {
		final String counthql = hql;
		Session session = sessionFactory.openSession();

		Query query = session.createQuery(counthql);
		query.setMaxResults(1);
		Long count = (Long) query.uniqueResult();
		if (!Hibernate.isInitialized(count))
			Hibernate.initialize(count);
		session.close();
		if (count != null) {
			return count.intValue();
		} else {
			return 0;
		}
	}

	public void delById(Class clazz, Serializable id) {
		Session session = sessionFactory.openSession();
		Object obj = session.load(clazz, id);
		Transaction tx = session.beginTransaction();
		session.delete(obj);
		tx.commit();
		session.close();
	}

	public List listAll(String sql) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from " + sql
				+ " as a order by a.ID desc");
		List list = query.list();
		if (!Hibernate.isInitialized(list))
			Hibernate.initialize(list);
		session.close();
		return list;
	}

	public List pageListAll(String clazz, int pageNo, int pageSize) {
		final int pNo = pageNo;
		final int pSize = pageSize;
		final String hql = "from " + clazz + " as a order by a.ID desc";
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(hql);
		query.setMaxResults(pSize);
		query.setFirstResult((pNo - 1) * pSize);
		List result = query.list();
		if (!Hibernate.isInitialized(result))
			Hibernate.initialize(result);
		List list = result;
		session.close();
		return list;
	}
	public List query(String hql,Integer sl) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(hql);
		query.setMaxResults(sl);
		query.setFirstResult(0);
		List result = query.list();
		if (!Hibernate.isInitialized(result))
			Hibernate.initialize(result);
		List list = result;
		session.close();
		return list;
	}
	public Object loadById(Class clazz, Serializable id) {
		Session session = sessionFactory.openSession();

		Object obj = session.get(clazz, id);
		if (!Hibernate.isInitialized(obj))
			Hibernate.initialize(obj);
		session.close();
		return obj;
	}

	public Object loadObject(String hql) {
		final String hql1 = hql;
		Object obj = null;
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(hql1);
		query.setMaxResults(1);

		List list = query.list();
		if (!Hibernate.isInitialized(list))
			Hibernate.initialize(list);
		session.close();
		if (list.size() > 0)
			obj = list.get(0);
		return obj;
	}

	public List query(String hql) {
		final String hql1 = hql;
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(hql1);
		List list = query.list();
		if (!Hibernate.isInitialized(list))
			Hibernate.initialize(list);
		session.close();
		return list;
	}

	public List pageQuery(String hql, int pageNo, int pageSize) {
		final int pNo = pageNo;
		final int pSize = pageSize;
		final String hql1 = hql;
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(hql1);
		query.setMaxResults(pSize);
		query.setFirstResult((pNo - 1) * pSize);
		List result = query.list();
		if (!Hibernate.isInitialized(result))
			Hibernate.initialize(result);
		session.close();
		return result;

	}

	public void saveOrUpdate(Object obj) {
		Session session = sessionFactory.openSession(); // ͨ��������������
		Transaction tx = session.beginTransaction();
		session.saveOrUpdate(obj);
		tx.commit();
		session.close();
	}

	public void save(Object obj) {
		Session session = sessionFactory.openSession(); // ͨ��������������
		Transaction tx = session.beginTransaction();
		session.save(obj); // ͨ�����ӱ���user
		tx.commit();
		session.close();

	}

	public int update(String hql) {
		final String hql1 = hql;
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		Query query = session.createQuery(hql1);
		Integer i = query.executeUpdate();
		tx.commit();
		session.close();
		return i.intValue();
	}

	public String getSingleValueBySql(String sql) {
		String result = "";
		Session session = sessionFactory.openSession();
		SQLQuery query = session.createSQLQuery(sql);
		List list = query.list();

		if (!list.isEmpty()) {
			result = (String) list.get(0);
		}
		session.close();
		return result;
	}

	public int getCountValueBySql(String sql) {
		sql = "SELECT COUNT(*) AS count_of_query FROM (" + sql + ") a";
		Session session = sessionFactory.openSession();
		SQLQuery q = session.createSQLQuery(sql);
		q.addScalar("count_of_query", StandardBasicTypes.INTEGER);
		Object o = (Object) q.uniqueResult();
		if (!Hibernate.isInitialized(o))
			Hibernate.initialize(o);
		session.close();
		return ((Integer) o).intValue();
	}

	public boolean executeSql(String sql) {
		Session session = sessionFactory.openSession();
		int i = session.createQuery(sql).executeUpdate();
		if (!Hibernate.isInitialized(i))
			Hibernate.initialize(i);
		session.close();
		if (i > 0) {
			return true;
		} else {
			return false;
		}

	}

	@Override
	public String getUver(String sql, Object[] args) {
		String result = "";
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(sql);
		query.setDate("Btime", (Date) args[0]);
		query.setDate("Etime", (Date) args[1]);
		query.setString("Sfzh", (String) args[2]);
		query.setMaxResults(1);
		String str = (String) query.uniqueResult();
		if (!Hibernate.isInitialized(str))
			Hibernate.initialize(str);
		session.close();
		return str;

	}

}
